<%@ page language="java" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<%
Object obj=request.getSession().getAttribute("role_resource");
if(null==obj){
	response.setStatus(1000);
	return;
}
%> 
    
	<script type="text/javascript">
	
		//交易代码列表数据源
		var code_store = new Ext.data.JsonStore({
    		root: "data",
    		totalProperty: "total",
    		fields: ["trancode","tranname","trantype","typename"],
    		url: "systemTranCode!selectSystemTranCode.action"
    	});
    	
    	//表单卡类型下拉列表列表数据源
		var type_store = new Ext.data.JsonStore({
    		root: "data",
    		totalProperty: "total",
    		fields : [ "id", "text"],
			url : "product!ProdType.action"
    	});
    	
    	//卡类型下拉列表列表数据源
		var type_store1 = new Ext.data.JsonStore({
    		root: "data",
    		totalProperty: "total",
    		fields : [ "id", "text"],
			url : "product!ProdType.action",
			listeners : {
				"load": function() {
					var RecordName = Ext.data.Record.create([
						{name: "id"},
						{name: "text"}
					]);
					var newRecord = new RecordName({
						text: "全部"
					});
					this.insert(0,newRecord);
				}
			}
    	});
    	
    	var sel = function(){
			code_store.baseParams = code_store.baseParams || {};

			code_store.baseParams["systemTranCode.trancode"] = Ext.getCmp("a").getValue();
			code_store.baseParams["systemTranCode.tranname"] = Ext.getCmp("b").getValue();
			code_store.baseParams["systemTranCode.trantype"] = Ext.getCmp("c").getValue();
						
			code_store.load({params:{start:0,limit:18}});
		};

		//定义数据显示面板
		var p1 = {
			id: "p1",
			xtype: "grid",
			stripeRows:true,
			loadMask : true,//在加载数据时遮罩住元素,替换元素Updater的加载提示器，并且在初始化加载之后销毁
			autoExpandColumn: "tranname",
			autoExpandMin : 100,//自动填充最小宽度
			store: code_store,
			headersDisabled: true,
			columns:[
	        	new Ext.grid.RowNumberer({header: "序号", width: 35}),
    					{header: "代码",	dataIndex: "trancode", menuDisabled: true},
    					{header: "名称",	dataIndex: "tranname", id: "tranname", menuDisabled: true},
    					{header: "类型",	dataIndex: "typename", menuDisabled: true}
			],
			buttonAlign: "center",
			bbar: new Ext.ux.PagingToolbar({
            	pageSize: 18,
            	store: code_store
        	}),
			buttons:[{text:"新增", handler: function(){
				oper_type = "add";
				Ext.getCmp("p2").resetParam();
				Ext.getCmp("mycontainer").layout.setActiveItem(1);
			}},{text:"修改", handler: function(){
				var record = Ext.getCmp("p1").getSelectionModel().getSelected();
				if (record){
					Ext.getCmp("p2").setParam(record.data);
					oper_type = "upd";
					Ext.getCmp("mycontainer").layout.setActiveItem(1);
				} else {
					//Ext.Msg.alert("警告","请先选择一条记录，然后点击修改！");
						Ext.Msg.show({
		                  title:"警告",
		                  msg: "请先选择一条记录，然后点击修改!",
		                  buttons: Ext.Msg.OK,
		                  animEl: "elId",
		                  icon:Ext.MessageBox.WARNING
		                  });
				}
			}},{text:"删除", handler: function(){
				var record = Ext.getCmp("p1").getSelectionModel().getSelected();
				if(record){
					Ext.Msg.confirm("警告","确定要删除吗?",function(button) {
						if(button == "yes") {
							Ext.Ajax.request({
								params: {
									"systemTranCode.trancode":record.data.trancode,
									"systemTranCode.tranname":record.data.tranname,
									"systemTranCode.trantype":record.data.trantype
								},
								url: "systemTranCode!deleteSystemTranCode.action",
								success: function(response, opts){
									var d=eval("("+response.responseText+")");
									if(d.code == "1"){
										Ext.getCmp("p1").getBottomToolbar().doRefresh();
										//Ext.Msg.alert("成功",d.data);
											Ext.Msg.show({
		                                        title:"提示",
		                                        msg:"操作成功!",
		                                        buttons:Ext.Msg.OK,
		                                        icon: Ext.MessageBox.INFO
		                                        });
									} else {
										//Ext.Msg.alert("失败",d.data);
											Ext.Msg.show({
		                                      title:"提示",
		                                      msg:"操作失败!",
		                                      buttons:Ext.Msg.OK,
		                                      icon: Ext.MessageBox.ERROR
		                                      });
									}
								}
							});
						}
					});
				} else {
					//Ext.Msg.alert("错误","没有任何行被选中，无法进行删除操作!");
						Ext.Msg.show({
		                    title:"警告",
		                    msg: "没有任何行被选中，无法进行删除操作!",
		                    buttons: Ext.Msg.OK,
		                    animEl: "elId",
		                    icon:Ext.MessageBox.WARNING
		                    });
				}
			}}],
    	   	tbar:["&nbsp&nbsp类型:",
			{ 	id: "c",
				xtype: "billCombox",
				width: 120,
				store: type_store1,
				listeners: {
					"select":function(){
						sel();
					}
				}
			},"&nbsp&nbsp代码:",
    	   	 {	id: "a",
				xtype: "textfield",
				maxLength: 10,
				enableKeyEvents: true,
				listeners: {
					"keypress":function(t,e){
						 if(13==e.getKey()){
							 sel();
						 }
					}
				}
			},"&nbsp&nbsp名称:",
			{	id: "b",
				xtype: "textfield",
				enableKeyEvents: true,
				listeners: {
					"keypress":function(t,e){
						 if(13==e.getKey()){
							 sel();
						 }
					}
				}
			}]
		};
		
		var p2 = {
			id: "p2",
		    labelWidth: 35,
			xtype: "form",
		    bodyStyle:"padding:5px 5px 0",
			defaultType: "textfield",
			items: [{
				id: "systemTranCode.trancode", 
				fieldLabel: "代码",	
				allowBlank: false,
				name: "systemTranCode.trancode",
				anchor: "100%",
				vtype:"alphanum",
				maxLength:20
			},{
				id: "systemTranCode.tranname", 
				fieldLabel: "名称",		
				allowBlank: false,
				name: "systemTranCode.tranname",
				anchor: "100%",
				vtype:"alphanumchinese",
				validator:function (val){
	        		if(val=="")return "该输入项为必输项！";
	        		if(Ext.fucCheckCHLength(val)>50){
	        			return "请输入50个字符范围内的值！<br>注：一个中文字符算两个字符的长度！";
	        		}
	        		return true;
	        	}
			},{
				id: "systemTranCode.trantype", 	  
				fieldLabel: "类型",		
				allowBlank: false,
				name: "systemTranCode.trantype",
				xtype: "billCombox",
				store: type_store
			}],
			buttonAlign: "center",
			buttons:[{text:"保存", 
			id:"save",
			handler: function(e){
				if(!Ext.getCmp("p2").getForm().isValid()){
					Ext.Msg.show({
	                    title:"提示",
	                    msg:"验证不通过!",
	                    buttons:Ext.Msg.OK,
	                    icon: Ext.MessageBox.ERROR
	                  });
					return;
				}
				e.setDisabled(true);
				var trancode = Ext.getCmp("systemTranCode.trancode").getValue();
				var tranname = Ext.getCmp("systemTranCode.tranname").getValue();
				var trantype = Ext.getCmp("systemTranCode.trantype").getValue();
				var url;
				if(oper_type == "add") {
					Ext.Ajax.request({	//AJAX验证主键唯一性
						clientValidation: true,
						params: {
							"systemTranCode.trancode": trancode,
							"systemTranCode.trantype": trantype
						},
						url: "systemTranCode!validKey.action",
						success: function(response, opts){
							var d=eval("("+response.responseText+")");
							if(d.code == "0"){
								Ext.Ajax.request({
									clientValidation: true,
									params: {
										"systemTranCode.trancode": trancode,
										"systemTranCode.tranname": tranname,
										"systemTranCode.trantype": trantype
									},
									url: "systemTranCode!insertSystemTranCode.action",
									success: function(response, opts){
										var d=eval("("+response.responseText+")");
										if(d.code == "1"){
											Ext.getCmp("p1").getBottomToolbar().doRefresh();
											Ext.Msg.show({
				                               title:"提示",
				                               msg:"操作成功!",
				                               buttons:Ext.Msg.OK,
				                               icon: Ext.MessageBox.INFO
				                             });
											Ext.getCmp("mycontainer").layout.setActiveItem(0);
										} else {
											Ext.Msg.show({
				                               title:"提示",
				                               msg:"操作失败!",
				                               buttons:Ext.Msg.OK,
				                               icon: Ext.MessageBox.ERROR
				                           });
										}
									}
								});
							} else {
								Ext.Msg.show({
	                               title:"提示",
	                               msg:"代码和类型已被占用!",
	                               buttons:Ext.Msg.OK,
	                               icon: Ext.MessageBox.ERROR
	                           });
							}
						
							Ext.getCmp("save").setDisabled(false);
						}
					});
				}else{
					url = "systemTranCode!updateSystemTranCode.action";
					Ext.Ajax.request({
						clientValidation: true,
						params: {
							"systemTranCode.trancode": trancode,
							"systemTranCode.tranname": tranname,
							"systemTranCode.trantype": trantype
						},
						url: url,
						success: function(response, opts){
							e.setDisabled(false); 
							var d=eval("("+response.responseText+")");
							if(d.code == "1"){
								Ext.getCmp("p1").getBottomToolbar().doRefresh();
								Ext.Msg.show({
	                               title:"提示",
	                               msg:"操作成功!",
	                               buttons:Ext.Msg.OK,
	                               icon: Ext.MessageBox.INFO
	                             });
								Ext.getCmp("mycontainer").layout.setActiveItem(0);
							} else {
								Ext.Msg.show({
	                               title:"提示",
	                               msg:"操作失败!",
	                               buttons:Ext.Msg.OK,
	                               icon: Ext.MessageBox.ERROR
	                           });
							}
						}
					});
				}
				}},{text:"取消", handler: function(){
						Ext.getCmp("mycontainer").layout.setActiveItem(0);
					}
				}],
			setParam: function(data){
				Ext.getCmp("systemTranCode.trancode").setValue(data.trancode);
				Ext.getCmp("systemTranCode.trancode").setDisabled(true);
				Ext.getCmp("systemTranCode.tranname").setValue(data.tranname);
				Ext.getCmp("systemTranCode.trantype").setValue(data.trantype);
				Ext.getCmp("systemTranCode.trantype").setDisabled(true);
			},
			resetParam: function(){
				Ext.getCmp("systemTranCode.trancode").setValue("");
				Ext.getCmp("systemTranCode.trancode").setDisabled(false);
				Ext.getCmp("systemTranCode.tranname").setValue("");
				Ext.getCmp("systemTranCode.trantype").setValue("");
				Ext.getCmp("systemTranCode.trantype").setDisabled(false);
			}
		};
		
		var tran_pel = new Ext.Panel({
			id: "mycontainer", 
			border : false,		
			layout: "card",
			activeItem: 0,
			items: [p1, p2]
		});
		code_store.load({params:{start:0,limit:18}});
		type_store.load();
		type_store1.load();
	
	
		var CenterPanel=Ext.getCmp("CenterPanel");
		CenterPanel.removeAll();
		CenterPanel.add(tran_pel);
		CenterPanel.doLayout();
	</script>
